package com.example.mybatisplus0621.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus0621.entity.Emp;
import com.example.mybatisplus0621.service.IEmpService;
import com.example.mybatisplus0621.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 洋洋
 * @since 2022-06-22
 */
@RestController
@RequestMapping("/emp")
@Slf4j
public class EmpController {

   @Autowired
   private IEmpService empService;

   /*查询所有员工
    * @return
    */
   @GetMapping("/getempAll")
   public JsonResult  getAll(){
      List<Emp> list = empService.list();
      return  JsonResult.success(list);
   }
   /**
    * 根据id查询单个员工
    * @param id
    * @return
    */
   @GetMapping("/getemp")
   public  JsonResult getEmp(Long id){
      Emp emp = empService.getById(id);
      return  JsonResult.success(emp);
   }
   /**
    * post请求 根据id查询单个员工
    * @param emp
    * @return
    */
   @PostMapping("/postemp")
   public  JsonResult postemp(@RequestBody Emp emp){
      LambdaQueryWrapper<Emp> wrapper=new LambdaQueryWrapper<>();
      wrapper.eq(Emp::getId,emp.getId());
      Emp empone = empService.getOne(wrapper);
      return  JsonResult.success(empone);
   }

   /**
    * 修改员工
    * @param emp
    * @return
    */
   @PostMapping("/updateemp")
   public  JsonResult updateemp(@RequestBody Emp emp){
//      LambdaUpdateWrapper<Emp> wrapper=new LambdaUpdateWrapper<>();
//      wrapper.eq(Emp::getId,emp.getId());
//      empService.update(wrapper);
      empService.updateById(emp);
      return  JsonResult.success("员工修改成功");
   }

   /**
    * 添加员工
    * @param emp
    * @return
    */
   @PostMapping("/saveemp")
   public  JsonResult saveemp(@RequestBody Emp emp){
       empService.save(emp);
       return  JsonResult.success("添加成功");
   }

   /**
    *POST方式 删除员工
    * @param
    * @return
    */
   @PostMapping("/deleteemp")
   public  JsonResult deleteemp(@RequestBody Emp emp){
       empService.removeById(emp);
     // empService.removeById(id);
      return  JsonResult.success("删除成功");
   }

   /**
    * GET方式 删除员工
    * @param id
    * @return
    */
   @GetMapping("/getdeleteemp")
   public  JsonResult getdeleteemp(Long id){
      empService.removeById(id);
      return  JsonResult.success("删除成功");
   }

   /**
    * 分页条件查询员工
    * @param page
    * @param pageSize
    * @param name
    * @return
    */
    @GetMapping("/page")
   public JsonResult page(int page, int pageSize,String name){
       log.info("==page=="+page+"==pageSize=="+pageSize+"==name=="+name);
       Page<Emp> pageinfo=new Page<>(page,pageSize);
       LambdaQueryWrapper<Emp> wrapper=new LambdaQueryWrapper<>();
       wrapper.like(StringUtils.isNotEmpty(name),Emp::getEname,name);
       wrapper.orderByDesc(Emp::getJoindate);
       Page<Emp> pagedate = empService.page(pageinfo, wrapper);
        return  JsonResult.success(pagedate);
    }




}
